package org.qydjk.console.controller.card;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

//用户体检卡
@Controller
@RequestMapping("/usercard")
public class UserCardController extends BaseController{

    @RequestMapping("/gotoUserCard.do")
    public String gotoCardCategory(){
        return "card/userCard";
    }

    /**
     * 用户体检卡数据表格
     * @param page
     * @param rows
     * @param code
     * @param username
     * @return
     */
    @RequestMapping("/userCarddg.do")
    @ResponseBody
    public Object userCarddg(Integer page, Integer rows,String code,String username){
        try {
            HibernatePage pager = new HibernatePage(page,rows);
            String sql="SELECT c.id,c.code,c.pwd,c.val,u.username,c.update_time,c.status FROM qy_user_store_card c ,qy_user u where c.user_Id = u.id";
            String countsql = "SELECT count(c.id) FROM qy_user_store_card c,qy_user u where c.user_Id = u.id";
            if(code!=null&&!"".equals(code)){
                sql+=" and c.code like '%"+code+"%'";
                countsql+=" and c.code like '%"+code+"%'";
            }
            if(username!=null&&!"".equals(username)){
                sql+=" and u.username like '%"+username+"%'";
                countsql+=" and u.username like '%"+username+"%'";
            }
            List<Map<String,Object>> classifylistList = baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countsql);
            return new EasyUIResponse(total,classifylistList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用户体检卡明细
     * @param page
     * @param rows
     * @param code
     * @param begin
     * @param end
     * @return
     */
    @RequestMapping("/cardDetaildg.do")
    @ResponseBody
    public Object cardDetaildg(Integer page, Integer rows,String code,String begin,String end){
        try {
            HibernatePage pager = new HibernatePage(page,rows);
            String sql="select id,code,pre_val,val,aft_val,type,seller_name,product_name,store_card_consume_detail_id,create_time from qy_user_store_card_detail where 1=1";
            String countsql = "select count(id) from qy_user_store_card_detail where 1=1";
            if(code!=null&&!"".equals(code)){
                sql+=" and code = '"+code+"'";
                countsql+=" and code = '"+code+"'";
            }
            if(begin!=null&&!"".equals(begin)){
                sql+=" and create_time > '"+begin+"'";
                countsql+=" and create_time > '"+begin+"'";
            }
            if(end!=null&&!"".equals(end)){
                sql+=" and create_time < '"+end+"'";
                countsql+=" and create_time < '"+end+"'";
            }
            List<Map<String,Object>> classifylistList =baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countsql);
            return new EasyUIResponse(total,classifylistList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //跳转消费记录
    @RequestMapping("/gotoConsume.do")
    public String gotoConsume(){
        return "card/consume";
    }

    @RequestMapping("/consumeDg.do")
    @ResponseBody
    public Object consumeDg(Integer page, Integer rows,String Id,String begin,String end,Integer type,String username ){
        try {
            HibernatePage pager = new HibernatePage(page,rows);
            String sql="select c.id,c.user_id,u.username,c.seller_name,c.product_name,c.val,c.settlement_record_id,c.settlement_type,c.create_time\n" +
                    "from qy_user_store_card_consume_detail c,qy_user u\n" +
                    "where c.user_id=u.id";
            String countsql = "select count(c.id) from qy_user_store_card_consume_detail c,qy_user u where c.user_id=u.id";
            if(Id!=null&&!"".equals(Id)){
                sql+=" and c.id = "+Id;
                countsql+=" and c.id = "+Id;
            }
            if(type!=null){
                sql+=" and c.settlement_type = "+type;
                countsql+=" and c.settlement_type = "+type;
            }
            if(username!=null&&!"".equals(username)){
                sql+=" and u.username like '%"+username+"%'";
                countsql+=" and u.username like '%"+username+"%'";
            }
            if(begin!=null&&!"".equals(begin)){
                sql+=" and c.create_time > '"+begin+"'";
                countsql+=" and c.create_time > '"+begin+"'";
            }
            if(end!=null&&!"".equals(end)){
                sql+=" and c.create_time < '"+end+"'";
                countsql+=" and c.create_time < '"+end+"'";
            }
            List<Map<String,Object>> classifylistList =baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countsql);
            return new EasyUIResponse(total,classifylistList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
